<!--
 * @Author: Allen
 * @Date: 2021-01-13 16:24:44
 * @Description: 抱怨身处黑暗，不如提灯前行。愿你在自己存在的地方，成为一束光，照亮世界的一角
 * @LastEditTime: 2021-02-04 16:03:57
 * @LastEditors: Allen
 * @FilePath: \WMS\src\views\WMS\claimDemageManage\claimDemageLaunch\edit.vue
 * @Additional: 附加类容
-->

<template>
  <section class="editCarBrand">
    <el-dialog
      v-dialogDrag
      :close-on-click-modal="false"
      :title="textMap[popupsState]"
      :append-to-body="true"
      :visible.sync="curPopupsVisible"
      @close="sendCode"
      width="1000px"
    >
      <dialogHeader
        slot="title"
        :title="textMap[popupsState]"
        :styleType="$store.state.app.dialogType"
      ></dialogHeader>
      <div class="filter-container filter-params">
        <el-row :gutter="26">
          <component
            v-for="comp in tableComponents.filter(o => o.isMust === true)"
            :ref="comp.isRequire ? comp.isRequire + comp.compKey : comp.compKey"
            :validrule="comp.validrule"
            :key="comp.compKey"
            :codeField="comp.codeField"
            :textField="comp.textField"
            :popupsKey="comp.compKey"
            :is="comp.component"
            :code="formField[comp.codeField]"
            :text="formField[comp.textField]"
            @changeCode="getComponentCode"
            @focusCode="getFocusCode"
            :disabled="comp.disabled"
            :isRequire="comp.isRequire"
            :span="comp.span || 8"
            :options="comp.options"
            :isMul="comp.isMul"
            :labelName="comp.labelName"
            :dateOptionsType="comp.dateOptionsType"
            :WAREHOUSE_ID="formField.WAREHOUSE_ID"
            @clear="editClear"
          ></component>
        </el-row>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button
          v-for="comp in tableButtons"
          :key="comp.compKey"
          :type="comp.type"
          @click="comp.clickEvent"
        >{{ comp.text }}</el-button>
      </div>
    </el-dialog>
  </section>
</template>
<script>
import { formMixins } from "@/components/wms/mixins/formMixins";
import { claimDemageAPI } from "@/api/wms/claimDemage";
import storage from "@/utils/storage";
import request from "@/utils/request";
export default {
  // 组件混入对象
  mixins: [formMixins],
  components: {
    dialogHeader: () => import("@/components/dialogHeader") //弹窗标题
  },
  watch: {
    popupsVisible(val) {
      if (val) {
        if (
          this.formField.pageId !== "" &&
          this.$route.query.pageId !== undefined &&
          this.$route.query.pageId !== this.formField.pageId
        ) {
          this.formField.pageId = this.$route.query.pageId;
          this.formField.pageName = this.$route.query.pageName;
        }
        this.formField.langLabelName = this.formField.label;
        this.backFormField = JSON.parse(JSON.stringify(this.formField));
      }
    }
  },
  props: {
    editData: {
      type: Object,
      default: {}
    }
  },
  data() {
    return {
      // 保存API配置对象
      apiConfig: claimDemageAPI.insertInitiateClaim,
      // 动态组件-按钮
      tableButtons: [
        {
          compKey: "btnKey1",
          type: "primary",
          size: "small",
          clickEvent: () => this.save(),
          text: "保存" //"保存"
        },
        {
          compKey: "btnKey2",
          type: "",
          size: "small",
          clickEvent: () => this.reset(),
          text: "重置" //"重置"
        }
      ],
      // 动态组件-查询条件
      tableComponents: [
        {
          compKey: "edit_compKey1",
          labelName: "仓库名称",
          codeField: "WAREHOUSE_ID",
          isRequire: true,
          component: () => import("@/components/wms/dropdownList/dropdownList"),
          type: "dropdownList",
          span: 8,
          isMust: true,
          isMul: false,
          options: [],
          changed: e => {
            this.tableComponents[6].options = [];
            this.formField.PLACE_CODE = "";
            this.formField.PART_NO = "";
            this.formField.PART_NAME = "";
            this.formField.NUM = "";
            if (!e || !this.formField.PART_NO) {
              return;
            }
            request({
              url: "/mp/wm/vl/warehouse/claim/queryPlaceBox.do",
              method: "POST",
              data: {
                WAREHOUSE_ID: e,
                PART_NO: this.formField.PART_NO
              }
            }).then(res => {
              if (
                res.listResult &&
                res.listResult.rows &&
                res.listResult.rows.length > 0
              ) {
                res.listResult.rows.map(item => {
                  this.tableComponents[6].options.push({
                    label: item.PLACE_CODE,
                    value: item.PLACE_CODE
                  });
                });
                this.selectList = res.listResult.rows;
              }
            });
          }
        },
        {
          compKey: "edit_compKey2",
          labelName: "发票号",
          span: 8,
          codeField: "INVOICE_NO",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isMust: true
        },
        {
          compKey: "edit_compKey3",
          labelName: "大箱号",
          span: 8,
          codeField: "BOX_NO",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isMust: true
        },
        {
          compKey: "edit_compKey4",
          labelName: "索赔类型",
          isRequire: true,
          codeField: "CLAIM_TYPE",
          component: () => import("@/components/wms/dropdownList/dropdownList"),
          type: "dropdownList",
          span: 8,
          isMust: true,
          isMul: false,
          options: [],
          changed: e => {}
        },
        {
          compKey: "edit_compKey5",
          labelName: "异常类型",
          isRequire: true,
          codeField: "ERROR_TYPE",
          component: () => import("@/components/wms/dropdownList/dropdownList"),
          type: "dropdownList",
          span: 8,
          isMust: true,
          isMul: false,
          options: [],
          changed: e => {}
        },
        {
          compKey: "edit_compKey6",
          labelName: "备件名称",
          textField: "PART_NAME",
          codeField: "PART_NO",
          isRequire: true,
          component: () =>
            import("@/components/wms/wmsEjectWindows/wmsPartInfoT2"),
          type: "propus",
          isMust: true,
          span: 8,
          isMul: false,
          isRequire: true,
          readonly: true,
          returnCodeField: "PART_NO",
          returnTextField: "PART_NAME",
          changed: e => {
            this.tableComponents[6].options = [];
            this.formField.PLACE_CODE = "";
            this.formField.NUM = "";
            request({
              url: "/mp/wm/vl/warehouse/claim/queryPlaceBox.do",
              method: "POST",
              data: {
                WAREHOUSE_ID: this.formField.WAREHOUSE_ID,
                PART_NO: e
              }
            }).then(res => {
              if (
                res.listResult &&
                res.listResult.rows &&
                res.listResult.rows.length > 0
              ) {
                res.listResult.rows.map(item => {
                  this.tableComponents[6].options.push({
                    label: item.PLACE_CODE,
                    value: item.PLACE_CODE
                  });
                });
                this.selectList = res.listResult.rows;
              }
            });
          }
        },
        {
          compKey: "edit_compKey7",
          labelName: "库位编号",
          isRequire: true,
          codeField: "PLACE_CODE",
          component: () => import("@/components/wms/dropdownList/dropdownList"),
          type: "dropdownList",
          span: 8,
          isMust: true,
          isMul: false,
          options: [],
          changed: e => {
            this.selectList.map(item => {
              if (item.PLACE_CODE == e) {
                this.formField.NUM = item.ACCOUNT_QTY;
              }
            });
          }
        },
        {
          compKey: "edit_compKey8",
          labelName: "库存数量",
          span: 8,
          disabled: true,
          codeField: "NUM",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isMust: true
        },
        {
          compKey: "edit_compKey9",
          labelName: "索赔数量",
          isRequire: true,
          span: 8,
          codeField: "CLAIM_QTY",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isRequire: true,
          isMust: true
        },
        {
          compKey: "edit_compKey10",
          labelName: "到货时间",
          span: 8,
          codeField: "DC_TIME",
          component: () => import("@/components/org/datePicker/datePicker"),
          type: "datePicker",
          isMust: true
        },
        {
          compKey: "edit_compKey11",
          labelName: "责任方",
          codeField: "RESPONSIBLE",
          component: () => import("@/components/wms/dropdownList/dropdownList"),
          type: "dropdownList",
          isRequire: true,
          span: 8,
          isMust: true,
          isMul: false,
          options: [],
          changed: e => {}
        },
        {
          compKey: "edit_compKey12",
          labelName: "来源单号",
          span: 8,
          codeField: "SOURCE_CODE",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isMust: true
        },
        {
          compKey: "edit_compKey13",
          labelName: "订单号",
          span: 8,
          codeField: "ORDER_CODE",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isMust: true
        },
        {
          compKey: "edit_compKey14",
          labelName: "订单行",
          span: 8,
          codeField: "ORDER_LINE_CODE",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isMust: true
        },
        {
          compKey: "edit_compKey15",
          labelName: "备件商品包装状况",
          span: 24,
          codeField: "PACKING_CONDITION",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isRequire: true,
          isMust: true
        },
        {
          compKey: "edit_compKey16",
          labelName: "运输包装状况",
          span: 24,
          codeField: "TRANSPORT_CONDITION",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isRequire: true,
          isMust: true
        },
        {
          compKey: "edit_compKey17",
          labelName: "异常原因分析",
          span: 24,
          codeField: "ABNORMAL_REASON",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isRequire: true,
          isMust: true
        },
        {
          compKey: "edit_compKey18",
          labelName: "备件受损/错件描述",
          span: 24,
          codeField: "ERROR_DESCRIBE",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isRequire: true,
          isMust: true
        }
      ],
      // 标题
      textMap: {
        edit: "编辑索赔",
        add: "编辑索赔"
      },
      // 表单数据（无需赋值，由混入对象赋值）
      formField: {
        CLAIM_ID: "",
        WAREHOUSE_ID: "",
        INVOICE_NO: "",
        BOX_NO: "",
        CLAIM_TYPE: "",
        ERROR_TYPE: "",
        PART_NO: "",
        PART_NAME: "",
        PLACE_CODE: "",
        NUM: "",
        CLAIM_QTY: "",
        DC_TIME: "",
        RESPONSIBLE: "",
        SOURCE_CODE: "",
        ORDER_CODE: "",
        ORDER_LINE_CODE: "",
        PACKING_CONDITION: "",
        TRANSPORT_CONDITION: "",
        ABNORMAL_REASON: "",
        ERROR_DESCRIBE: ""
      },
      selectList: []
    };
  },
  methods: {
    editClear() {
      this.tableComponents[6].options = [];
      this.formField.PLACE_CODE = "";
      this.formField.NUM = "";
    },
    onCreated() {
      if (this.curPopupsVisible) {
        // this.formField.WAREHOUSE_ID = this.tableComponents[0].options[0].value;
        // 仓库
        this.editData.UserByWHBox.map(item => {
          this.tableComponents[0].options.push({
            label: item.WAREHOUSE_NAME,
            value: item.WAREHOUSE_ID
          });
        });
        // 索赔类型
        this.editData.ClaimType.map(item => {
          this.tableComponents[3].options.push({
            label: item.CLAIM_NAME,
            value: item.CLAIM_TYPE
          });
        });
        // 异常类型
        this.editData.ErrorType.map(item => {
          this.tableComponents[4].options.push({
            label: item.ERROR_NAME,
            value: item.ERROR_TYPE
          });
        });
        // 责任方
        this.editData.Responsible.map(item => {
          this.tableComponents[10].options.push({
            label: item.RESPONSIBLE_NAME,
            value: item.RESPONSIBLE
          });
        });
        if (this.isAdd() == false) {
          request({
            url: "/mp/wm/vl/warehouse/claim/queryPlaceBox.do",
            method: "POST",
            data: {
              WAREHOUSE_ID: this.formField.WAREHOUSE_ID,
              PART_NO: this.formField.PART_NO
            }
          }).then(res => {
            this.tableComponents[6].options = [];
            if (
              res.listResult &&
              res.listResult.rows &&
              res.listResult.rows.length > 0
            ) {
              res.listResult.rows.map(item => {
                this.tableComponents[6].options.push({
                  label: item.PLACE_CODE,
                  value: item.PLACE_CODE
                });
              });
              this.selectList = res.listResult.rows;
              this.selectList.map(item => {
                if (item.PLACE_CODE == this.formField.PLACE_CODE) {
                  this.formField.NUM = item.ACCOUNT_QTY;
                }
              });
            } else {
              this.formField.PLACE_CODE = "";
              this.formField.NUM = "";
            }
          });
        }
      }
    },
    //保存
    save() {
      if (parseInt(this.formField.MOVE_QTY) > this.formField.AVAILABLE_NUM) {
        this.formField.MOVE_QTY = "1";
        this.$message.error("抱歉！移库数量超出限制~");
        return false;
      }
      this.$utils.validataMoth(this, "validpopup");
      if (this.valid) {
        var saveObj = JSON.parse(JSON.stringify(this.formField));
        //保存表单
        this.saveForm(saveObj, e => {
          if (e && e.result === "3") {
            this.$confirm(e.msg, "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning"
            })
              .then(() => {
                saveObj.IS_MAIN = 1;
                this.saveForm(saveObj);
              })
              .catch(() => {});
          }
        });
        storage.remove(this.$route.query.pageCode);
      }
    }
  }
};
</script>
<style type="text/css" scoped>
.filter-params .el-col label .curName {
  width: 150px;
}
</style>
